<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RedPacketRain extends BaseModel
{
    protected $connection = 'api';
    protected $rprtable = 'red_packet_rain';
    protected $rprlTable = 'red_packet_rain_log';
    protected $userTable = 'user';
    protected $room_table = 'room';//房间表
    protected $union_room = 'union_room';//公会房间
    protected $union = 'union';//公会表

    //红包雨新增
    public function redPacketRainAdd($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/redPacketRainAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //红包雨编辑
    public function redPacketRainEdit($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/redPacketRainEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //红包雨删除
    public function redPacketRainDel($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/redPacketRainDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //红包雨列表
    public function redPacketRainList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $rain_id = $request->input('rain_id', 0);
        $date_st = $request->input('date_st', '');
        $date_ed = $request->input('date_ed', '');

        $query = DB::connection($this->connection)->table($this->rprtable)->orderBy('id', 'desc');
        if(!empty($rain_id)) {
            $query = $query->where('id', $rain_id);
        }
        if(!empty($date_st)) {
            $time_st = date("Y-m-d H:i:s", strtotime($date_st));
            $query = $query->where('start_time', '>=', $time_st);
        }
        if(!empty($date_ed)) {
            $time_ed = date("Y-m-d H:i:s", strtotime($date_ed));
            $query = $query->where('start_time', '<=', $time_ed);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);

        if(!empty($result['data'])) {
            $adminIds = array_column($result['data'], 'admin_id');
            $adminList = DB::table('users')->whereIn('id', $adminIds)->select(['id','name'])->get();
            $adminList = array_column(objToArray($adminList), null, 'id');

            foreach($result['data'] as $key=>$val) {
                $val['admin_name'] = empty($adminList[$val['admin_id']]['name']) ? '' : $adminList[$val['admin_id']]['name'];

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //红包雨记录列表
    public function redPacketRainLogList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $user_id = $request->input('user_id', 0);
        $date_st = $request->input('date_st', '');
        $date_ed = $request->input('date_ed', '');

        $query = DB::connection($this->connection)->table($this->rprlTable.' as a')
            ->join($this->rprtable.' as b', "a.rain_id", "=", "b.id")
            ->orderBy('a.id', 'desc')
            ->select(['a.*','b.start_time']);
        if(!empty($user_id)) {
            $query = $query->where('a.user_id', $user_id);
        }
        if(!empty($date_st)) {
            $time_st = date("Y-m-d H:i:s", strtotime($date_st));
            $query = $query->where('b.start_time', '>=', $time_st);
        }
        if(!empty($date_ed)) {
            $time_ed = date("Y-m-d H:i:s", strtotime($date_ed));
            $query = $query->where('b.start_time', '<=', $time_ed);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        if(!empty($result['data'])) {
            $userIds = array_column($result['data'], 'user_id');
            $userList = DB::connection($this->connection)->table($this->userTable)->whereIn('user_id', $userIds)->get();
            $userList = array_column(objToArray($userList), null, 'user_id');

            foreach($result['data'] as $key=>$val) {
                $val['nickname'] = empty($userList[$val['user_id']]['nickname']) ? '' : $userList[$val['user_id']]['nickname'];
                $val['uuid'] = empty($userList[$val['user_id']]['uuid']) ? '' : $userList[$val['user_id']]['uuid'];

                $result['data'][$key] = $val;
            }
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

}
